/*
 * EnigmaGM - A feature rich guild management system.
 * Copyright (C) 2007 ajiaojr.
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */
package org.ajiaojr.enigmagm.client.dialog;

import org.ajiaojr.enigmagm.client.UserSessionManager;
import org.ajiaojr.enigmagm.client.event.UserEventAdapter;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class RegisterDialog extends DialogBox {

  private PasswordTextBox passwordTextBox;

  private PasswordTextBox retypeTextBox;

  private UserEventAdapter userEventAdapter = new UserEventAdapter() {

    public void onRegistrationFailure(Throwable cause) {
      Window.alert(cause.getMessage());
    }

    public void onRegistrationSuccess() {
      Window
          .alert("Registration successful. Please wait for your account to be activated.");
      RegisterDialog.this.hide();
    }

  };

  private TextBox usernameTextBox;

  public RegisterDialog() {
    super(false);
    setHTML("Register");

    final VerticalPanel verticalPanel = new VerticalPanel();
    setWidget(verticalPanel);
    verticalPanel.setWidth("260px");

    final HorizontalPanel horizontalPanel = new HorizontalPanel();
    verticalPanel.add(horizontalPanel);
    horizontalPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
    horizontalPanel.setWidth("100%");
    horizontalPanel.setSpacing(5);

    final Label usernameLabel = new Label("Username");
    horizontalPanel.add(usernameLabel);
    horizontalPanel.setCellHorizontalAlignment(usernameLabel,
        HasHorizontalAlignment.ALIGN_RIGHT);
    horizontalPanel.setCellWidth(usernameLabel, "80px");

    usernameTextBox = new TextBox();
    horizontalPanel.add(usernameTextBox);

    final HorizontalPanel horizontalPanel_1 = new HorizontalPanel();
    verticalPanel.add(horizontalPanel_1);
    horizontalPanel_1.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
    horizontalPanel_1.setWidth("100%");
    horizontalPanel_1.setSpacing(5);

    final Label passwordLabel = new Label("Password");
    horizontalPanel_1.add(passwordLabel);
    horizontalPanel_1.setCellHorizontalAlignment(passwordLabel,
        HasHorizontalAlignment.ALIGN_RIGHT);
    horizontalPanel_1.setCellWidth(passwordLabel, "80px");

    passwordTextBox = new PasswordTextBox();
    horizontalPanel_1.add(passwordTextBox);

    final HorizontalPanel horizontalPanel_2 = new HorizontalPanel();
    verticalPanel.add(horizontalPanel_2);
    horizontalPanel_2.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
    horizontalPanel_2.setWidth("100%");
    horizontalPanel_2.setSpacing(5);

    final Label retypeLabel = new Label("Re-type");
    horizontalPanel_2.add(retypeLabel);
    horizontalPanel_2.setCellHorizontalAlignment(retypeLabel,
        HasHorizontalAlignment.ALIGN_RIGHT);
    horizontalPanel_2.setCellWidth(retypeLabel, "80px");

    retypeTextBox = new PasswordTextBox();
    horizontalPanel_2.add(retypeTextBox);

    final HorizontalPanel horizontalPanel_3 = new HorizontalPanel();
    verticalPanel.add(horizontalPanel_3);
    horizontalPanel_3.setSpacing(5);
    verticalPanel.setCellHorizontalAlignment(horizontalPanel_3,
        HasHorizontalAlignment.ALIGN_CENTER);

    final Button submitButton = new Button();
    horizontalPanel_3.add(submitButton);
    submitButton.addClickListener(new ClickListener() {
      public void onClick(Widget sender) {
        if (validateInput()) {
          UserSessionManager.getInstance().register(usernameTextBox.getText(),
              passwordTextBox.getText());
        }
      }

      private boolean validateInput() {
        if (usernameTextBox.getText().length() < 6) {
          Window.alert("Username need to be at least 6 characters long");
          return false;
        }
        if (passwordTextBox.getText().length() < 6) {
          Window.alert("Password need to be at least 6 characters long");
          return false;
        }
        if (!retypeTextBox.getText().equals(passwordTextBox.getText())) {
          Window.alert("Your password doesn't match the re-type");
          return false;
        }
        return true;
      }
    });
    submitButton.setText("Submit");

    final Button cancelButton = new Button();
    horizontalPanel_3.add(cancelButton);
    cancelButton.addClickListener(new ClickListener() {
      public void onClick(Widget sender) {
        RegisterDialog.this.hide();
      }
    });
    cancelButton.setText("Cancel");

    UserSessionManager.getInstance().addUserEventListener(userEventAdapter);

    setPopupPosition((Window.getClientWidth() - 260) / 2, (Window
        .getClientHeight() - 300) / 2);
  }

  public void hide() {
    UserSessionManager.getInstance().removeUserEventListener(userEventAdapter);
    super.hide();
  }
}
